package com.manager.action.admin;

import java.util.List;
import java.util.Map;

import org.common.tool.FillBeanUtils;
import org.common.util.TranslateHelper;
import org.wface.web.core.ActionSupport;

import com.manager.model.SysEvent;
import com.manager.model.SysRole;
import com.manager.service.system.AdminService;
import com.web.tag.bean.PageModel;

public class EventAction extends ActionSupport {

	private String page;
	private String id;
	private String[] check;

	public String list() {
		eventListPage(null);
		return "eventList";
	}

	public String edit() {
		AdminService adminService = new AdminService();
		SysEvent event = adminService.findEventById(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("editEvent", event);
		// *******************
		eventListPage(adminService);
		return "eventList";
	}

	public String save() {
		SysEvent event = new SysEvent();
		@SuppressWarnings("unchecked")
		Map<String, String[]> parameterMap = getRequest().getParameterMap();
		new FillBeanUtils(parameterMap).injectBean(event);
		AdminService adminService = new AdminService();
		if (adminService.saveEvent(event)) {
			getRequest().setAttribute("saveMsg", "系统事件保存成功!");
		} else {
			getRequest().setAttribute("saveMsg", "系统事件保存失败!");
		}
		getRequest().setAttribute("editEvent", event);
		// *******************
		eventListPage(adminService);
		return "eventList";
	}

	public String delete() {
		AdminService adminService = new AdminService();
		if (adminService.deleteEvent(TranslateHelper.parseInteger(id))) {
			getRequest().setAttribute("saveMsg", "系统事件删除成功!");
		} else {
			getRequest().setAttribute("saveMsg", "系统事件删除失败!");
		}
		// *******************
		eventListPage(adminService);
		return "eventList";
	}

	public String roleEvent() {
		roleEvent(null);
		return "role-event";
	}

	public String roleEventSave() {
		AdminService adminService = new AdminService();
		if (adminService.saveRoleEvent(TranslateHelper.parseInteger(id), check)) {
			getRequest().setAttribute("saveMsg", "系统角色对应事件设置成功!");
		} else {
			getRequest().setAttribute("saveMsg", "系统角色对应事件设置失败!");
		}
		// ********************
		roleEvent(adminService);
		return "role-event";
	}

	private void eventListPage(AdminService adminService) {
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);
		if (adminService == null)
			adminService = new AdminService();
		List<SysEvent> events = adminService.findAllEvent(model);
		getRequest().setAttribute("eventList", events);
		model.setPaths(getBasePath() + "/manager/event/list.do?");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
	}

	private void roleEvent(AdminService adminService) {
		if (adminService == null)
			adminService = new AdminService();
		List<SysRole> roles = adminService.findAllRole(null, 1);
		List<SysEvent> events = adminService.findAllEvent(null, 1);
		getRequest().setAttribute("roleList", roles);
		getRequest().setAttribute("eventList", events);
		SysRole role = adminService.findRoleEvent(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("selectRole", role);
		// System.out.println(role.getEvents().size());
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String[] getCheck() {
		return check;
	}

	public void setCheck(String[] check) {
		this.check = check;
	}

}
